// QuadState.cpp: implementation of the CQuadState class.
//CQuadPlayState
//////////////////////////////////////////////////////////////////////

#include "../common.h"
#include "QuadState.h"


#pragma warning (disable: 4018) // 有符号/无符号不匹配

CQuadPlayState::CQuadPlayState(ArrayQuad3D& rQuad3DSlides, int icenterSlide) : m_rQuad3DSlides(rQuad3DSlides)
{
    //
    m_clrBackGround.red =    0;
    m_clrBackGround.green =  0;
    m_clrBackGround.blue =   0;
    m_clrBackGround.alpha =  0;
    m_enReflectionEffect =   true;

    //
    m_angle =    0;
    m_spacing =  0;
    m_offsetX =  0;
    m_offsetZ =  0;

    //无效 = -1
    m_centerSlide = icenterSlide;
    m_leftSlides.clear();
    m_rightSlides.clear();

    //
    Init();
}

//
CQuadPlayState::~CQuadPlayState()
{
    //
    m_centerSlide = -1;
    m_leftSlides.clear();
    m_rightSlides.clear();
}

//
void CQuadPlayState::CenterOffsetPos(float fAxisAngleFactor)
{
    float   fRad, fDeg;
    float   fx, fy, fz;

    ASSERT(fAxisAngleFactor <= 1 && fAxisAngleFactor >= -1);

    if (m_centerSlide < 0)
    {
        return;
    }

    //绕 y轴旋转中央粒子
    fDeg = DEGREE_COVER * fAxisAngleFactor;
    fRad = DEG2RAD(fDeg);
    fx = m_offsetX + DEPTH_Z * sin(fRad);
    fy = 0;
    fz = DEPTH_Z * cos(fRad);
    m_rQuad3DSlides[m_centerSlide]->SetPosition(CVector(fx, fy, fz));
    m_rQuad3DSlides[m_centerSlide]->SetAngle(fDeg);
}

//
void CQuadPlayState::SlidesRotateOffsetPos(int iIndex, float fAxisAngleFactor)
{
    bool    blLeft = true;
    float   fRad, fDeg;
    float   fx, fy, fz;
    float   fx1, fy1, fz1;
    float   fRad1 = 0.0f,
            fDeg1 = 0.0f;
    int npos;
    ASSERT(fAxisAngleFactor <= 1 && fAxisAngleFactor >= -1);

    if (iIndex == m_centerSlide ||
            iIndex >= m_rQuad3DSlides.size() ||
            iIndex < 0 ||
            fAxisAngleFactor == 0)
    {
        return;
    }

    //绕 y轴旋转边缘粒子
    if (true)
    {
        if (fAxisAngleFactor > 0)
        {
            blLeft = true;
        }
        else
        {
            blLeft = false;
        }

        fDeg = DEGREE_COVER * fAxisAngleFactor;
        fRad = DEG2RAD(fDeg);
        fDeg1 = 180 * fAxisAngleFactor;
        fRad1 = DEG2RAD(fDeg1);

        if (blLeft)
        {
            fz = DEPTH_Z * sin(fRad);
            fy = 0;
            fx = DEPTH_Z * cos(fRad);
            m_rQuad3DSlides[iIndex]->SetPosition(CVector(fx, fy, fz));
            m_rQuad3DSlides[iIndex]->SetAngle(DEGREE_COVER - fDeg);
            //旋转最后一个
            fz1 = m_offsetX - DEPTH_Z * sin(fRad1) * m_rQuad3DSlides.size() - 1;
            fy1 = 0;
            fx1 = m_offsetZ - DEPTH_Z * cos(fRad1) * m_rQuad3DSlides.size() + 4;
            npos = iIndex - 3;//写死了,左边3个

            if (npos < 0)
            {
#pragma warning (disable: 4267) // warning C4267: “+=”: 从“size_t”转换到“int”，可能丢失数据
                npos += m_rQuad3DSlides.size();
#pragma warning (default: 4267) // warning C4267: “+=”: 从“size_t”转换到“int”，可能丢失数据
            }

            m_rQuad3DSlides[npos]->SetPosition(CVector(fx1, fy1, fz1));
            m_rQuad3DSlides[npos]->SetAngle(-fDeg1);
        }

        else
        {
            fz = m_offsetX - DEPTH_Z * sin(fRad);
            fy = 0;
            fx = m_offsetZ - DEPTH_Z * cos(fRad);
            m_rQuad3DSlides[iIndex]->SetPosition(CVector(fx, fy, fz));
            m_rQuad3DSlides[iIndex]->SetAngle(DEGREE_COVER - fDeg + 180);
            //旋转最后一个
            fz1 = DEPTH_Z * sin(fRad1) * m_rQuad3DSlides.size() - 2;
            fy1 = 0;
            fx1 = DEPTH_Z * cos(fRad1) * m_rQuad3DSlides.size() - 3;
            npos = iIndex + 3;//写死了,右边3个.

            if (npos >= m_rQuad3DSlides.size())
            {
#pragma warning (disable: 4267)
                npos -= m_rQuad3DSlides.size();
#pragma warning (default: 4267)
            }

            m_rQuad3DSlides[npos]->SetPosition(CVector(fx1, fy1, fz1));
            m_rQuad3DSlides[npos]->SetAngle(-fDeg1);
        }
    }
}

//
void CQuadPlayState::SlidesOffsetPos(CVector& rV3OffsetFactor)
{
    int     i, ii;
    float   fx, fy, fz, fRad;

    fRad = DEG2RAD(m_angle);

    //左边排
    for (i = 0; i < (int)m_leftSlides.size(); i++)
    {
        ii = m_leftSlides[i];
        fx = -(m_offsetX + m_spacing * i + DISTANCE_COVER * tan(fRad)) - rV3OffsetFactor.x * m_spacing;
        fy = 0;

        if (i == 0)
        {
            fz = m_offsetZ;
        }
        else
        {
            fz = i * DEPTH_INTERVAL;
        }

        m_rQuad3DSlides[ii]->SetPosition(CVector(fx, fy, fz));
    }

    //右边排
    for (i = 0; i < (int)m_rightSlides.size(); i++)

    {
        ii = m_rightSlides[i];
        fx = (m_offsetX + m_spacing * i + DISTANCE_COVER * tan(fRad)) - rV3OffsetFactor.x * m_spacing;
        fy = 0;

        if (i == 0)
        {
            fz = m_offsetZ;
        }
        else
        {
            fz = i * DEPTH_INTERVAL;
        }

        m_rQuad3DSlides[ii]->SetPosition(CVector(fx, fy, fz));
    }
}
//
void CQuadPlayState::Init()
{
    //
    m_angle = 20;
    m_offsetX = 0;
    m_offsetZ = 0;
    m_spacing = DISTANCE_INTERVAL;
}

// adjust slides so that they are in "steady state" position
void CQuadPlayState::reset()
{
    float   fx, fy, fz;
    int     ii, i = 0;

    //define pos
    //中心
    if (m_centerSlide >= 0)
    {
        fx = m_offsetX;
        fy = 0;
        fz = DEPTH_Z;
        m_rQuad3DSlides[m_centerSlide]->SetPosition(CVector(fx, fy, fz));
        m_rQuad3DSlides[m_centerSlide]->SetAngle(0);
        m_rQuad3DSlides[m_centerSlide]->SetCenter(CENTER);
        //      m_rQuad3DSlides[m_centerSlide]->SetBlendingMode(0);
    }

    //左边排
    for (i = 0; i < m_leftSlides.size(); i++)
    {
        ii = m_leftSlides[i];
        fx = -(m_offsetX + m_spacing * i + DISTANCE_COVER);
        fy = 0;

        if (i == 0)
        {
            fz = m_offsetZ;
        }
        else
        {
            fz = i * DEPTH_INTERVAL;
        }

        m_rQuad3DSlides[ii]->SetPosition(CVector(fx, fy, fz));
        m_rQuad3DSlides[ii]->SetAngle(m_angle);

    }

    //右边排
    for (i = 0; i < m_rightSlides.size(); i++)
    {
        ii = m_rightSlides[i];
        fx = (m_offsetX + m_spacing * i + DISTANCE_COVER - 0.2);
        fy = 0;

        if (i == 0)
        {
            fz = m_offsetZ;
        }
        else
        {
            fz = i * DEPTH_INTERVAL;
        }

        m_rQuad3DSlides[ii]->SetPosition(CVector(fx, fy, fz));
        m_rQuad3DSlides[ii]->SetAngle(-m_angle);
    }
}


//
int CQuadPlayState::AddParticle(bool isLeft, CQuad3D* pQuad)
{
    int i = -1;
    ASSERT(pQuad != NULL);

    m_rQuad3DSlides.push_back(pQuad);
#pragma warning (disable: 4267) // size_t -> int, maybe lost data
    i = m_rQuad3DSlides.size() - 1;
#pragma warning (default: 4267)

    if (isLeft)
    {
        m_leftSlides.push_back(i);
    }

    else
    {
        m_rightSlides.push_back(i);
    }

    return i;
}


//
void CQuadPlayState::DelParticle(bool isLeft, int iIndex)
{
    std::vector<int>::iterator   it;

    //
    if (isLeft)
    {
        for (it = m_leftSlides.begin(); it != m_rightSlides.end(); ++it)
            if (*it == iIndex)
            {
                m_leftSlides.erase(it);
                break;
            }
    }

    else
    {
        for (it = m_rightSlides.begin(); it != m_rightSlides.end(); ++it)
            if (*it == iIndex)
            {
                m_rightSlides.erase(it);
                break;
            }
    }
}


//
void CQuadPlayState::SetCenterParticle(bool isLeft, int iNO)
{
    int i, ii;

    ASSERT(iNO >= 0);

    //中心粒子和其边缘粒子群重连接
    if (isLeft)
    {
        //中心粒子
        if (m_centerSlide >= 0)
        {
            m_rightSlides.insert(m_rightSlides.begin(), m_centerSlide);
        }

        //边缘粒子群
        for (i = 0; i < iNO; i++)
        {
            ii = m_leftSlides[0];
            m_rightSlides.insert(m_rightSlides.begin(), ii);
            m_leftSlides.erase(m_leftSlides.begin());
        }

        //iNO
        ii = m_leftSlides[0];
        m_leftSlides.erase(m_leftSlides.begin());
    }

    else
    {
        //中心粒子
        if (m_centerSlide >= 0)
        {
            m_leftSlides.insert(m_leftSlides.begin(), m_centerSlide);
        }

        //边缘粒子群
        for (i = 0; i < iNO; i++)
        {
            ii = m_rightSlides[0];
            m_leftSlides.insert(m_leftSlides.begin(), ii);
            m_rightSlides.erase(m_rightSlides.begin());
        }

        //iNO
        ii = m_rightSlides[0];
        m_rightSlides.erase(m_rightSlides.begin());
    }

    //中心粒子设置
    m_centerSlide = ii;
}

//
void CQuadPlayState::SetCenterParticle(int iIndex)
{
    bool    isLeft;
    int i, ii;

    //
    if (iIndex == m_centerSlide)
    {
        return;
    }

    //边缘粒子群
    isLeft = true;

    for (i = 0; i < m_leftSlides.size(); i++)
    {
        ii = m_leftSlides[i];

        if (ii == iIndex)
        {
#pragma warning (disable: 4267) // size_t to int
            SetCenterParticle(isLeft, i);
#pragma warning (default: 4267)
            return;
        }
    }

    //
    isLeft = false;

    for (i = 0; i < m_rightSlides.size(); i++)
    {
        ii = m_rightSlides[i];

        if (ii == iIndex)
        {
#pragma warning (disable: 4267) // size_t to int
            SetCenterParticle(isLeft, i);
#pragma warning (default: 4267)
            return;
        }
    }
}


//
int CQuadPlayState::QuerySlideDir(int iIndex)
{
    int i, ii;

    //
    if (iIndex == m_centerSlide)
    {
        return 0;
    }

    //边缘粒子群
    for (i = 0; i < m_leftSlides.size(); i++)
    {
        ii = m_leftSlides[i];

        if (ii == iIndex)
        {
            return -1;
        }
    }

    //
    for (i = 0; i < m_rightSlides.size(); i++)
    {
        ii = m_rightSlides[i];

        if (ii == iIndex)
        {
            return 1;
        }
    }

    //
    return 0;
}


void CQuadPlayState::Render()
{
    int i, ii;
#pragma warning (disable: 4018) // “<”: 有符号/无符号不匹配

    //中心
    if (m_centerSlide >= 0)
    {
        m_rQuad3DSlides[m_centerSlide]->Render();

    }

    //左边排
    for (i = 0; i < m_leftSlides.size(); ++i)
    {
        ii = m_leftSlides[i];

        if (i == 0)
        {
            m_rQuad3DSlides[ii]->SetCenter(LEFTSLIDER);
        }
        else
        {
            m_rQuad3DSlides[ii]->SetCenter(OTHER);
        }

        //
        m_rQuad3DSlides[ii]->Render();

    }

    //右边排
    for (i = 0; i < m_rightSlides.size(); ++i)
    {
        ii = m_rightSlides[i];

        //
        if (i == 0)
        {
            m_rQuad3DSlides[ii]->SetCenter(RIGHTSLIDER);
        }
        else
        {
            m_rQuad3DSlides[ii]->SetCenter(OTHER);
        }

        m_rQuad3DSlides[ii]->Render();

    }

#pragma warning (default: 4018) // “<”: 有符号/无符号不匹配
}

int CQuadPlayState::click(int x, int y)
{
    int i, nID = 0;

    for (i = 0; i < (int)m_rQuad3DSlides.size(); i++)
    {
        nID = m_rQuad3DSlides[i]->Click(x, y);

        if (nID != -1)
        {
            return nID;
        }
    }

    return -1;
}



#pragma warning (default: 4018) // 有符号/无符号不匹配